#!/bin/bash

#-------------------------
# 功能：为xcode工程打ipa包
# 使用方法：
# 1、把脚本拖入终端.
# 2、把项目拖进也拖进终端.
# 3、在最后输入你要打包的ipa名称（可不写），回车就 OK.
#
# 注意事项：如果没有成功报出问题:
# Permission denied。就是没有权限.
#
# 解决办法：
# 1、为其添加可执行权限，使用命令：chmod +x shell_ipa.sh (shell_ipa.sh 为脚本绝对路径).
# 2、修改该文件 脚本.sh 的权限，使用命令： 
# sudo chmod 777 脚本的绝对目录.
# 然后再执行就 OK.
#-------------------------

#参数判断
if [ $# != 2 ] && [ $# != 1 ];then
 echo "Number of params error! Need one or two params!"
 echo "1.path of project(necessary) 2.name of ipa file(optional)"
 exit 
elif [ ! -d $1 ];then
 echo "Params Error!! The first param must be a dictionary."
 exit 
fi

#工程绝对路径
cd $1
project_path=$(pwd)
#build文件夹路径
build_path=${project_path}/build

#工程配置文件路径
project_name=$(ls | grep xcodeproj | awk -F.xcodeproj '{print $1}')
project_infoplist_path=${project_path}/${project_name}/${project_name}-Info.plist
#取版本号
bundleShortVersion=$(/usr/libexec/PlistBuddy -c "print CFBundleShortVersionString" ${project_infoplist_path})
#取build值
bundleVersion=$(/usr/libexec/PlistBuddy -c "print CFBundleVersion" ${project_infoplist_path})
#取bundle Identifier前缀
#bundlePrefix=$(/usr/libexec/PlistBuddy -c "print CFBundleIdentifier" `find . -name "*-Info.plist"` | awk -F$ '{print $1}')


#IPA名称
if [ $# = 2 ];then
ipa_name=$2
fi


#编译工程
cd $project_path
xcodebuild || exit

#打包
cd $build_path
target_name=$(basename ./Release-iphoneos/*.app | awk -F. '{print $1}')
if [ $# = 1 ];then
ipa_name="${target_name}_${bundleShortVersion}_build${bundleVersion}_$(date +"%Y%m%d_%H%M")"
fi

if [ -d ./ipa-build ];then
 rm -rf ipa-build
fi
mkdir -p ipa-build/Payload
cp -r ./Release-iphoneos/*.app ./ipa-build/Payload/
cd ipa-build
zip -r ${ipa_name}.ipa *
rm -rf Payload